#pragma once

#include <vector>
#include <string>
#include <ctemplate/template.h>
#include "oj_model.hpp"

namespace ns_view
{
    using namespace ns_model;

    const std::string questionPath="./template_html/";
    class View
    {
    public:
        View() {}
        ~View() {}

    public:
        void AllExpand(const std::vector<Question> &questions, std::string *html)
        {
            // 1. 形成路径
            std::string src_html = "./template_html/all_questions.html";
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 2. 创建数据字典
            ctemplate::TemplateDictionary dict("all_questions");
            for (const auto &q : questions)
            {
                ctemplate::TemplateDictionary *sub_dict = dict.AddSectionDictionary("question_list");
                sub_dict->SetValue("number", q.number);
                sub_dict->SetValue("title", q.title);
                sub_dict->SetValue("star", q.star);
                sub_dict->SetValue("star_color", GetStarColor(q.star));
                sub_dict->SetValue("cpu_limit", std::to_string(q.cpu_limit));
                sub_dict->SetValue("mem_limit", std::to_string(q.mem_limit));
            }

            // 3. 开始渲染
            tpl->Expand(html, &dict);
        }
        void OneExpand(const Question &q, std::string *html)
        {
            std::string src_html = "./template_html/question.html";
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            ctemplate::TemplateDictionary dict("question");
            // 基本字段
            dict.SetValue("number", q.number);
            dict.SetValue("title", q.title);
            dict.SetValue("star", q.star);
            dict.SetValue("star_color", GetStarColor(q.star));
            dict.SetValue("desc", q.desc);
            dict.SetValue("test_input", q.test_input);
            dict.SetValue("test_output", q.test_output);
            dict.SetValue("cpu_limit", std::to_string(q.cpu_limit));
            dict.SetValue("mem_limit", std::to_string(q.mem_limit));

            // 用于编辑表单的字段
            dict.SetValue("question_number", q.number);  // 用于编辑时的问题编号
            dict.SetValue("question_title", q.title);    // 纯标题文本
            dict.SetValue("question_star", q.star);      // 难度级别
            dict.SetValue("question_desc", q.desc);      // 问题描述
            dict.SetValue("test_input_data", q.test_input);    // 测试输入
            dict.SetValue("test_output_data", q.test_output);  // 测试输出
            dict.SetValue("cpu_limit_value", std::to_string(q.cpu_limit));  // CPU限制
            dict.SetValue("mem_limit_value", std::to_string(q.mem_limit));  // 内存限制

            tpl->Expand(html, &dict);
        }

        void RenderSubmissions(const std::vector<UserModel::Submission> &subs, std::string *html)
        {
            std::string src_html = "./template_html/user_submissions.html";
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            ctemplate::TemplateDictionary dict("submissions");
            for (const auto &sub : subs)
            {
                ctemplate::TemplateDictionary *sub_dict = dict.AddSectionDictionary("submission_list");
                sub_dict->SetValue("number", sub.number);
                sub_dict->SetValue("title", sub.title);
                sub_dict->SetValue("star", sub.star);
                sub_dict->SetValue("status", sub.status);
                sub_dict->SetValue("submit_time", sub.submit_time);
            }

            tpl->Expand(html, &dict);
        }

        // 渲染论坛页面
        void RenderForum(const std::string &question_number, const std::vector<ForumPost> &posts, std::string *html)
        {
            std::string src_html = "./template_html/forum.html";
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            ctemplate::TemplateDictionary dict("forum");
            dict.SetValue("question_number", question_number);

            for (const auto &post : posts)
            {
                ctemplate::TemplateDictionary *post_dict = dict.AddSectionDictionary("posts");
                post_dict->SetValue("id", std::to_string(post.id));
                post_dict->SetValue("title", post.title);
                post_dict->SetValue("user_id", post.user_id);
                post_dict->SetValue("create_time", post.create_time);
                post_dict->SetValue("update_time", post.update_time);
            }

            tpl->Expand(html, &dict);
        }

        // 渲染帖子详情页面
        void RenderPostDetail(const ForumPost &post, const std::vector<ForumComment> &comments, std::string *html)
        {
            std::string src_html = "./template_html/post_detail.html";
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            ctemplate::TemplateDictionary dict("post_detail");
            dict.SetValue("id", std::to_string(post.id));
            dict.SetValue("title", post.title);
            dict.SetValue("content", post.content);
            dict.SetValue("user_id", post.user_id);
            dict.SetValue("create_time", post.create_time);
            dict.SetValue("update_time", post.update_time);
            dict.SetValue("question_number", std::to_string(post.question_number));

            for (const auto &comment : comments)
            {
                ctemplate::TemplateDictionary *comment_dict = dict.AddSectionDictionary("comments");
                comment_dict->SetValue("user_id", comment.user_id);
                comment_dict->SetValue("content", comment.content);
                comment_dict->SetValue("create_time", comment.create_time);
            }

            tpl->Expand(html, &dict);
        }

    private:
        std::string GetStarColor(const std::string &star)
        {
            if (star == "简单")
                return "success";
            else if (star == "中等")
                return "warning";
            else
                return "danger";
        }
    };
}